package com.example.template.core.service.impl;


import com.example.template.core.dao.BaseDao;
import com.example.template.core.dao.impl.BaseDaoImpl;
import com.example.template.core.entity.BaseEntity;
import com.example.template.core.service.BaseService;
import com.example.template.core.utils.GenericeClassUtils;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.Mapper;

import javax.annotation.Resource;
import java.util.List;

@Service("myBaseService")
public class MyBatisBaseServiceImpl<T extends BaseEntity> implements BaseService<T> {

    @SuppressWarnings("unchecked")
    protected Class<T> entityClass = (Class<T>) GenericeClassUtils.getSuperClassGenricType(this.getClass(), 0);


    @Resource(name = "myBatisBaseDao")
    private BaseDao<T> baseDao;

    @Override
    public T getEntityById(Integer id) {
        return baseDao.getEntityById(entityClass, id);
    }

    @Override
    public int addEntity(T entity) {
        return baseDao.addEntity(entity);
    }

    @Override
    public int updateEntity(T entity) {
        return baseDao.updateEntity(entity);
    }

    @Override
    public int deleteEntityById(Integer id) {
        return baseDao.deleteEntityById(entityClass,id);
    }

    @Override
    public List<T> selectAll(final T t, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return baseDao.selectAll(entityClass,t);
    }
}
